package com.zego.wawaji.message;

import com.zego.wawaji.base.log.PLogger;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.socket.IMProxy;
import com.zego.wawaji.socket.NetData;
import com.zego.wawaji.socket.TCPConstant;
import com.zego.wawaji.socket.listener.IMListener;
import com.zego.wawaji.ui.view.adDialog.bean.AdInfo;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 接收消息处理类
 * Created by Kind on 2017/4/1.
 */
public class RecMessageMgr implements IMListener {

    public void init() {
        IMProxy.getInstance().attach(this);
    }

    public void release() {
        IMProxy.getInstance().detach(this);
    }

    @Override
    public void onMessage(NetData data) {
        try {
            PLogger.d("reMsg-jsonStr=" + data.getMsgBody());
            JSONObject object = new JSONObject(data.getMsgBody());
            if (data.getFromId() <= 0) {//如果小于或等于0
                data.setFromId(object.optLong("IdFrom"));
            }
            int type = data.getMsgType();
            if (type == TCPConstant.MSG_WAWAJI_STATUS) { // 娃娃机房间状态消息
                String content = data.getContent();
                MsgMgr.getInstance().sendMsg(MsgType.MT_Msg_RoomChange, content);
            }

            if (type == TCPConstant.MSG_NOTIFICATION) {  // 广告推送消息
                adMsg(data.getContent());
            }

        } catch (Exception e) {
            PLogger.printThrowable(e);
        }
    }

    /**
     * 广告推送消息
     */
    private void adMsg(final String content) {
        MsgMgr.getInstance().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    if (jsonObject == null) return;
                    AdInfo adInfo = new AdInfo();
                    adInfo.setTitle(jsonObject.optString("Title"));
                    adInfo.setActivityImg(jsonObject.optString("Img"));
                    adInfo.setUrl(jsonObject.optString("Url"));

                    List<AdInfo> list = new ArrayList<>();
                    list.add(adInfo);
                    ModuleMgr.getWawaMgr().createAdMgr(list);
                } catch (Exception e) {
                }
            }
        });
    }

    /**
     * 广播推送采用三方服务
     */
    private void notifyMsg(String content) {
//        if (TextUtils.isEmpty(content)) return;
//        try {
//            JSONObject jsonObject = new JSONObject(content);
//            String title = jsonObject.optString("Title");
//            String img = jsonObject.optString("Img");
//            String url = jsonObject.optString("Url");
//            String text = jsonObject.optString("Content");
//
//            Intent intent = new Intent(App.getContext(), MainActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//            PendingIntent pIntent = PendingIntent.getActivity(App.getContext(),
//                    (int) SystemClock.uptimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
//            NotifyUtil.getInstance().notify_normal_singline(pIntent, title, text, img);
//
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }
}